﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MachineBuildingSystem
{
    public partial class MaterialCrossForm : Form
    {
        public MaterialCrossForm()
        {
            InitializeComponent();
        }

        private DataTable m_BindDT = null;

        private List<MaterialInfo> m_MaterialInfoList = new List<MaterialInfo>();
        public List<MaterialInfo> MaterialInfoList
        {
            set { m_MaterialInfoList = value; }
        }

        private Dictionary<string, string> m_MaterialCrossDic = new Dictionary<string, string>();

        private void MaterialCrossForm_Load(object sender, EventArgs e)
        {
            InitBindDT();
            m_MaterialCrossDic = new Dictionary<string, string>();
            try
            {
                string materialCrossStr = ConfigurationManager.AppSettings["MaterialCross"].ToString();
                if (!string.IsNullOrEmpty(materialCrossStr))
                {
                    foreach (string item in materialCrossStr.Split(','))
                    {
                        string[] strArr = item.Split('(');
                        string key = strArr[0];
                        string value = strArr[1].TrimEnd(')');
                        if (m_MaterialCrossDic.ContainsKey(key))
                            continue;
                        m_MaterialCrossDic.Add(key, value);
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("获取配置中物料对照关系异常：" + ex.Message);
                return;
            }        
            RefreshBindDT();
        }

        private void InitBindDT()
        {
            m_BindDT = new DataTable();
            m_BindDT.Columns.Add("MaterialCode", typeof(string));
            m_BindDT.Columns.Add("SystemName", typeof(string));
            m_BindDT.Columns.Add("MachineName", typeof(string));
        }

        private void RefreshBindDT()
        {
            m_BindDT.Rows.Clear();
            gdcMaterialCross.DataSource = m_BindDT;
            try
            {
                foreach (MaterialInfo material in m_MaterialInfoList)
                {
                    string machineName = "";
                    if (m_MaterialCrossDic.ContainsKey(material.materialCode))
                        machineName = m_MaterialCrossDic[material.materialCode];
                    string displayName = material.materialSstd == "" ? material.materialName
                        : (material.materialName + "(" + material.materialSstd + ")");
                    m_BindDT.Rows.Add(material.materialCode, displayName, machineName);
                }
            }
            catch (Exception ex)
            {
                LogHelper.error("刷新物料对照表异常：" + ex.Message);
                return;
            }
            gdcMaterialCross.DataSource = m_BindDT;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> tempDic = new Dictionary<string, string>();
            for (int i = 0; i < gdvMaterialCross.RowCount; i++)
            {
                DataRow row = gdvMaterialCross.GetDataRow(i);
                tempDic.Add(row[0].ToString(), row[2].ToString());
            }
            string materialCrossStr = "";
            foreach (string systemName in tempDic.Keys)
            {
                materialCrossStr += systemName + "(" + tempDic[systemName] + ")" + ",";
            }
            materialCrossStr = materialCrossStr.TrimEnd(',');
            Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            cfa.AppSettings.Settings["MaterialCross"].Value = materialCrossStr;//设置config的值
            cfa.Save();
            ConfigurationManager.RefreshSection("appSettings");
            MessageBox.Show(this, "保存物料对照信息成功！");
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
